#include "stdafx.h"
BOOL APIENTRY DllMain( 
    HMODULE hModule,
    DWORD  ul_reason_for_call,
    LPVOID lpReserved)
{
    return TRUE;
}

ULONG __stdcall InitializeEtw(const char* loggerName, const char* etlFileName)
{
    StringCchCopyA(LoggerName, sizeof(LoggerName), loggerName);
    StringCchCopyA(EtlLoggerFileName, sizeof(EtlLoggerFileName), etlFileName);
    Register();
    SessionInitialize(IN OUT loggerdata);
    SessionStart(loggerdata);
    SessionEnable();
    return NO_ERROR;
}
ULONG __stdcall WriteEtw(const char* message)
{
    ULONG retcode = FireBasicFormat(message);
    return retcode;
}

ULONG __stdcall StopEtw()
{
    SessionDisable();
    ControlStop(loggerdata);
    Unregister();
    return NO_ERROR;
}
ULONG __stdcall DecodeEtw(const char* loggerName, const char* etlFileName, const char* txtFileName)
{
    ReadWholeTrace(loggerName, etlFileName, txtFileName);
    return NO_ERROR;
}

JNIEXPORT jint JNICALL Java_microsoft_tracing_Loglib_initializeEtw (JNIEnv *env, jclass, jstring JloggerName, jstring JetlFileName)
{
    const char* loggerName = (const char*)(env)->GetStringUTFChars(JloggerName, 0);
    const char* etlFileName = (const char*)(env)->GetStringUTFChars(JetlFileName, 0);
	UINT retcode = InitializeEtw(loggerName, etlFileName);
    env->ReleaseStringUTFChars (JetlFileName, etlFileName);
    env->ReleaseStringUTFChars (JloggerName, loggerName);
	return (jint)retcode;
}

JNIEXPORT jint JNICALL Java_microsoft_tracing_Loglib_writeEtw (JNIEnv *env, jclass, jstring jmessage)
{
    const char* message = (const char*)(env)->GetStringUTFChars(jmessage, 0);
	UINT retcode = WriteEtw(message);
    env->ReleaseStringUTFChars (jmessage, message);
	return (jint)retcode;
}

JNIEXPORT jint JNICALL Java_microsoft_tracing_Loglib_stopEtw (JNIEnv *env, jclass)
{
	UINT retcode = StopEtw();
	return (jint)retcode;
}

JNIEXPORT jint JNICALL Java_microsoft_tracing_Loglib_decodeEtw (JNIEnv *env, jclass, jstring jloggerName, jstring jetlFileName, jstring jtxtFileName)
{
    const char* loggerName = (const char*)(env)->GetStringUTFChars(jloggerName, 0);
    const char* etlFileName = (const char*)(env)->GetStringUTFChars(jetlFileName, 0);
    const char* txtFileName = (const char*)(env)->GetStringUTFChars(jtxtFileName, 0);
	UINT retcode = DecodeEtw(loggerName, etlFileName, txtFileName);
    env->ReleaseStringUTFChars (jtxtFileName, txtFileName);
    env->ReleaseStringUTFChars (jetlFileName, etlFileName);
    env->ReleaseStringUTFChars (jloggerName, loggerName);
	return (jint)retcode;
}




